export default {
	config: {
		baseURL: 'http://localhost:8080',
		//baseURL: 'http://127.0.0.1:9991',
		getToken() {
			let token = uni.getStorageSync('userToken');

			if (!token) {
				var pages = getCurrentPages();
				const currentPage = pages[pages.length - 1];
				const path = currentPage.route;

				if (path == "pages/changyong/index" || path != 'pages/index/register' || path !=
					'pages/changyong/login') {

				} else {
					
					return uni.redirectTo({
						url: '/pages/index/login'
					});
					
				}
			}

			return token
		},
		// 请求拦截器
		beforeRequest(options = {}) {
			return new Promise((resolve, reject) => {
				options.url = this.baseURL + options.url;
				options.method = options.method || 'GET';
				options.data = options.data;


				if (options.method == 'GET') {
					options.header = {
						"token": this.getToken(),
						"Authorization": "Bearer " + this.getToken(),
					}
				} else {
					options.header = {
						//加上下面这句目的是避免后端接受的参数为null
						'content-type': 'application/x-www-form-urlencoded',
						//'content-type': 'application/json;charset=UTF-8',
						"Authorization": "Bearer " + this.getToken(),
						"token": this.getToken()
					}
				}

				resolve(options)
			})
		},
		// 响应拦截器
		handleResponse(data) {
			return new Promise((resolve, reject) => {
				//const [err, res] = data;
				/*
                if (res && res.statusCode !== 200) {
                    let msg = res.data.msg || '请求错误';
                    uni.showToast({
                        icon: 'none',
                        title: msg
                    })
                    return reject(msg)
                }
                if (err) {
                    uni.showToast({
                        icon: 'none',
                        title: '请求错误'
                    })
                    return reject(err)
                }
				*/
				//return resolve(res.data)
			})
		},

	},
	request(options = {}) {
		return this.config.beforeRequest(options).then(opt => {
			return uni.request(opt)
		}) //.then(res => this.config.handleResponse(res))
	}
}